import 'package:flutter/material.dart';
import '../pages/tabs/Tabs.dart';
import '../pages/test/Search.dart';
import '../pages/tabs/ProductList.dart';

// 配置路由
// final routes = {'/': (context) => Tabs(), '/search': (context) => SearchPage()};
// 下面都是固定写法了
// var onGenerateRoute = (RouteSettings settings) {
//   // 统一处理
//   // final String name = settings.name;
//   final String? name = settings.name;
//   // final Function pageContentBuilder = routes[name];
//   final Function pageContentBuilder = routes[name] as Function;
//   if (pageContentBuilder != null) {
//     if (settings.arguments != null) {
//       final Route route = MaterialPageRoute(
//           builder: (context) =>
//               pageContentBuilder(context, arguments: settings.arguments));
//       return route;
//     } else {
//       final Route route =
//           MaterialPageRoute(builder: (context) => pageContentBuilder(context));
//       return route;
//     }
//   }
// };

// ignore: top_level_function_literal_block
// var  onGenerateRoute = (RouteSettings settings) {
// // 统一处理
// // ignore: top_level_function_literal_block
//   final String? name = settings.name;
// // ignore: top_level_function_literal_block
//   final Function pageContentBuilder = routes[name] as Function;
//   print('到1');
//   // if (pageContentBuilder != null) {
//   print('到2');
//     if (settings.arguments != null) {
//   print('到3');
//       final Route route = MaterialPageRoute(
//           builder: (context) =>
//               pageContentBuilder(context, arguments: settings.arguments));
//       return route;
//     } else {
//       final Route route =
//           MaterialPageRoute(builder: (context) => pageContentBuilder(context));
//       return route;
//     }
//   // }
// };

final Map<String, WidgetBuilder> router = {
  //配置路由路径
  '/': (context, {arguments}) => Tabs(arguments: arguments), //使用arguments传递参数
  // '/login': (context, {arguments}) => Login()
  '/search': (context) => SearchPage(),
  '/productList': (context, {arguments}) =>
      ProductListPage(arguments: arguments)
};
// ignore: top_level_function_literal_block
Route<dynamic> onGenerateRoute = (RouteSettings settings) {
  //onGenerateRoute配置
  final String? name = settings.name;
  final Function pageContentBuilder = router[name] as Function;
  // ignore: unnecessary_null_comparison
  if (pageContentBuilder != null) {
    if (settings.arguments != null) {
      final Route route = MaterialPageRoute(
          builder: (context) =>
              pageContentBuilder(context, arguments: settings.arguments));
      return route;
    } else {
      final Route route =
          MaterialPageRoute(builder: (context) => pageContentBuilder(context));
      return route;
    }
  }
} as Route;

// final Map<String, WidgetBuilder> routes = {
//     '/': (context) => Tabs(),
//     '/search': (context) => SearchPage(),
//   };

//   Route<dynamic> _onGenerateRoute(RouteSettings settings) {
//     //   return MaterialPageRoute(builder: (context) {
//     //     // 如果访问的路由页需要登录，但当前未登录，则直接返回登录页路由，
//     //     // 引导用户登录；其它情况则正常打开路由。
//     //     // 统一处理
//     // })
//     final String? name = settings.name;
//     final Function pageContentBuilder = routes[name] as Function;
//     print('到1');
//     if (settings.arguments != null) {
//       print('到2');
//       final Route route = MaterialPageRoute(
//         builder: (context) => pageContentBuilder(context, arguments: settings.arguments)),
//       );
//       return route;
//     } else {
//       print('到3');
//       final Route route = MaterialPageRoute(
//         builder: (context) {
//           return ParamPage(text: ModalRoute.of(context).settings.arguments);
//         },
//       );
//       return route;
//     }
//   }